<?php
/**
 * Created by PhpStorm.
 * User: zhengze
 * Date: 2019/7/1
 * Time: 18:29
 */

namespace app\common\traits;

use app\common\model\base\BaseModel;

/**
 * Trait ServiceTrait
 * @package common\traits
 * @property BaseModel $model
 */
trait ServiceTrait
{
    /**
     * @var BaseModel
     */
    protected $model = NULL;

    /**
     * 获取列表
     * @param $param
     * @return mixed|\think\Model|\think\model\Pivot|null
     * @throws \think\exception\DbException
     */
    public function getList($param)
    {
        $data = $this->model->getList($param);
        return $data;
    }

    /**
     * 获取统计
     * @param $param
     * @return mixed|\think\Model|\think\model\Pivot|null
     */
    public function getCount($param)
    {
        $data = $this->model->getCount($param);
        return $data;
    }

    /**
     * 获取ID
     * @param $param
     * @return BaseModel|null
     */
    public function getByID($param)
    {
        $id = !empty($param['id']) ? $param['id'] : $param[$this->model->getPk()];
        $data = $this->model->getByID($id);
        return $data;
    }

    /**
     * 插入数据
     * @param $param
     * @return bool
     */
    public function createData($param)
    {
        $data = $this->model->createData($param);
        return $data;
    }

    /**
     * 更新数据
     * @param $param
     * @return bool
     */
    public function updateByID($param)
    {
        $id = !empty($param['id']) ? $param['id'] : $param[$this->model->getPk()];
        $data = $this->model->updateByID($param, $id);
        return $data;
    }

    /**
     * 删除数据
     * @param $param
     * @return bool
     */
    public function delByID($param)
    {
        $id = !empty($param['id']) ? $param['id'] : $param[$this->model->getPk()];
        $data = $this->model->delByID($id, FALSE);
        return $data;
    }

    /**
     * 批量删除
     * @param $param
     * @return bool
     */
    public function delByIDs($param)
    {
        $ids = !empty($param['ids']) ? $param['ids'] : $param[$this->model->getPk() . 's'];
        $data = $this->model->delByIDs($ids, FALSE);
        return $data;
    }

    /**
     * 启用禁用
     * @param $param
     * @return bool
     */
    public function enableByIDs($param)
    {
        $ids = !empty($param['ids']) ? $param['ids'] : $param[$this->model->getPk() . 's'];
        $data = $this->model->enableByIDs($ids, $param['status'], FALSE);
        return $data;
    }
}
